﻿@page "/tags"

@inject ViewDataService ViewDataService
@inject IUserStatusService statusService
@inject ITagSearchService tagSearchService

<div class="damselfly-tagview">
    @if( Tags == null )
    {
        <p>
            <em>Loading tags...</em>
        </p>
    }
    else
    {
        <MudTable Items="@Tags" FixedHeader="true" RowsPerPage="20" Virtualize="true" Dense="true" Hover="true" Bordered="false" Striped="true"
                  Filter="new Func<Tag, bool>(FilterFunc)" @bind-SelectedItem="selectedTag">
            <ToolBarContent>
                <NavBack/>
                <MudSpacer/>
                <MudText Typo="Typo.h6">Keyword Tags</MudText>
                <MudSpacer/>
                <ConflatedTextBox IconClass="fa-search" id="searchBox" placeholder="Search" OnValueChanged="@DoSearch" TextValue="@searchText"/>
            </ToolBarContent>
            <HeaderContent>
                <MudTh>
                    <MudTableSortLabel InitialDirection="SortDirection.Ascending" SortBy="new Func<Tag, object>(x => x.Keyword)">Keyword</MudTableSortLabel>
                </MudTh>
                <MudTh>
                    <MudTableSortLabel SortBy="new Func<Tag, object>(x => x.Favourite)">Favourite</MudTableSortLabel>
                </MudTh>
                <MudTh>
                    <MudTableSortLabel SortBy="new Func<Tag, object>(x => x.TimeStamp)">Date Created</MudTableSortLabel>
                </MudTh>
            </HeaderContent>
            <RowTemplate>
                <MudTd DataLabel="Keyword">
                    <NavLink href="@TagLink(context)">@context.Keyword</NavLink>
                </MudTd>
                <MudTd DataLabel="Favourite?">@context.Favourite</MudTd>
                <MudTd DataLabel="Date Created">@context.TimeStamp</MudTd>
            </RowTemplate>
            <PagerContent>
                <MudTablePager/>
            </PagerContent>
        </MudTable>
    }
</div>

@code {

    [Parameter] public string TagName { get; set; }

    Tag selectedTag;
    string searchText;

    private string TagLink(Tag tag)
    {
        return $"/?tagid={tag.TagId}";
    }

    private List<Tag> Tags;

    protected override void OnInitialized()
    {
        _ = LoadTags();

        base.OnInitialized();
    }

    private async Task LoadTags()
    {
        statusService.UpdateStatus("Loading tags...");
        var results = await tagSearchService.GetAllTags();
        Tags = results.ToList();
        StateHasChanged();
    }

    private void DoSearch(string searchTerm)
    {
        searchText = searchTerm;
        StateHasChanged();
    }


    private bool FilterFunc(Tag tag)
    {
        if( string.IsNullOrEmpty(searchText) )
            return true;

        return tag.Keyword.Contains(searchText, StringComparison.OrdinalIgnoreCase);
    }

    protected override void OnAfterRender(bool firstRender)
    {
        if( firstRender )
        {
            ViewDataService.SetSideBarState(new ViewDataService.SideBarState { ShowBasket = true, ShowTags = true });
        }
    }

}